package org.bcncoding.appengine.droiding.server.dao;

import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class AbstractDao<T> {
	protected PersistenceManager pm = PMF.get().getPersistenceManager();
	protected Class<T> type = null;

	public AbstractDao(Class<T> type) {
		this.type = type;
	}

	public T get(Long id) {
		Key k = KeyFactory.createKey(type.getSimpleName(), id);
		return pm.getObjectById(type, k);
	}

	@SuppressWarnings("unchecked")
	public List<T> findAll() {
		String query = "SELECT FROM " + type.getName();
		return (List<T>) pm.newQuery(query).execute();
	}

	public T save(T obj) {
		return pm.makePersistent(obj);
	}

	public void refresh(T obj) {
		pm.refresh(obj);
	}
	
	public void delete(T obj) {
		pm.deletePersistent(obj);
	}
}
